package com.spdzen.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spdzen.service.SysResourceService;
import com.spdzen.support.AdminLoginInfo;
import com.spdzen.support.AdminLoginUtils;
import com.spdzen.support.LogUtils;
import com.spdzen.support.result.JsonResult;

@Controller
@RequestMapping("/admin/frame")
public class FrameAdminController {
	@Resource
	private SysResourceService sysResourceService;

	@RequestMapping("/main.do")
	public String mainView(HttpServletRequest request) {
		return "/admin/frame/main";
	}
	
	@RequestMapping("/getTopMenus.do")
	@ResponseBody
	public JsonResult getTopMenus(HttpServletRequest request) {
		
		try {
			AdminLoginInfo info = AdminLoginUtils.getLoginInfo();
			return JsonResult.success(sysResourceService.selectTopMenus(info.getUserId()));
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			return JsonResult.error();
		}
	}
	
	@RequestMapping("/getSubMenus.do")
	@ResponseBody
	public JsonResult getSubMenus(Long pid, HttpServletRequest request) {
		if(pid == null || pid.longValue() < 0){
			return JsonResult.errorParameter();
		}
		
		try {
			AdminLoginInfo info = AdminLoginUtils.getLoginInfo();
			return JsonResult.success(sysResourceService.selectSubTreeByUser(info.getUserId(), pid, true));
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			return JsonResult.error();
		}
	}
}
